home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Software / Freeware / Programare / bluej / bluejsetup-203.exe / {app} / examples / shapes / Square.java < prev    next >
Text File  |  2004-12-19  |  3KB  |  192 lines

  1. import java.awt.*;
  2.  
  3. /**
  4.  * A square that can be manipulated and that draws itself on a canvas.
  5.  * 
  6.  * @author    Michael Kolling and David J. Barnes
  7.  * @version 1.0  (15 July 2000)
  8.  */
  9.  
  10. public class Square
  11. {
  12.     private int size;
  13.     private int xPosition;
  14.     private int yPosition;
  15.     private String color;
  16.     private boolean isVisible;
  17.  
  18.     /**
  19.      * Create a new square at default position with default color.
  20.      */
  21.     public Square()
  22.     {
  23.         size = 30;
  24.         xPosition = 60;
  25.         yPosition = 50;
  26.         color = "red";
  27.         isVisible = false;
  28.     }
  29.  
  30.     /**
  31.      * Make this square visible. If it was already visible, do nothing.
  32.      */
  33.     public void makeVisible()
  34.     {
  35.         isVisible = true;
  36.         draw();
  37.     }
  38.     
  39.     /**
  40.      * Make this square invisible. If it was already invisible, do nothing.
  41.      */
  42.     public void makeInvisible()
  43.     {
  44.         erase();
  45.         isVisible = false;
  46.     }
  47.     
  48.     /**
  49.      * Move the square a few pixels to the right.
  50.      */
  51.     public void moveRight()
  52.     {
  53.         moveHorizontal(20);
  54.     }
  55.  
  56.     /**
  57.      * Move the square a few pixels to the left.
  58.      */
  59.     public void moveLeft()
  60.     {
  61.         moveHorizontal(-20);
  62.     }
  63.  
  64.     /**
  65.      * Move the square a few pixels up.
  66.      */
  67.     public void moveUp()
  68.     {
  69.         moveVertical(-20);
  70.     }
  71.  
  72.     /**
  73.      * Move the square a few pixels down.
  74.      */
  75.     public void moveDown()
  76.     {
  77.         moveVertical(20);
  78.     }
  79.  
  80.     /**
  81.      * Move the square horizontally by 'distance' pixels.
  82.      */
  83.     public void moveHorizontal(int distance)
  84.     {
  85.         erase();
  86.         xPosition += distance;
  87.         draw();
  88.     }
  89.  
  90.     /**
  91.      * Move the square vertically by 'distance' pixels.
  92.      */
  93.     public void moveVertical(int distance)
  94.     {
  95.         erase();
  96.         yPosition += distance;
  97.         draw();
  98.     }
  99.  
  100.     /**
  101.      * Slowly move the square horizontally by 'distance' pixels.
  102.      */
  103.     public void slowMoveHorizontal(int distance)
  104.     {
  105.         int delta;
  106.  
  107.         if(distance < 0) 
  108.         {
  109.             delta = -1;
  110.             distance = -distance;
  111.         }
  112.         else 
  113.         {
  114.             delta = 1;
  115.         }
  116.  
  117.         for(int i = 0; i < distance; i++)
  118.         {
  119.             xPosition += delta;
  120.             draw();
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Slowly move the square vertically by 'distance' pixels.
  126.      */
  127.     public void slowMoveVertical(int distance)
  128.     {
  129.         int delta;
  130.  
  131.         if(distance < 0) 
  132.         {
  133.             delta = -1;
  134.             distance = -distance;
  135.         }
  136.         else 
  137.         {
  138.             delta = 1;
  139.         }
  140.  
  141.         for(int i = 0; i < distance; i++)
  142.         {
  143.             yPosition += delta;
  144.             draw();
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Change the size to the new size (in pixels). Size must be >= 0.
  150.      */
  151.     public void changeSize(int newSize)
  152.     {
  153.         erase();
  154.         size = newSize;
  155.         draw();
  156.     }
  157.  
  158.     /**
  159.      * Change the color. Valid colors are "red", "yellow", "blue", "green",
  160.      * "magenta" and "black".
  161.      */
  162.     public void changeColor(String newColor)
  163.     {
  164.         color = newColor;
  165.         draw();
  166.     }
  167.  
  168.     /*
  169.      * Draw the square with current specifications on screen.
  170.      */
  171.     private void draw()
  172.     {
  173.         if(isVisible) {
  174.             Canvas canvas = Canvas.getCanvas();
  175.             canvas.draw(this, color,
  176.                         new Rectangle(xPosition, yPosition, size, size));
  177.             canvas.wait(10);
  178.         }
  179.     }
  180.  
  181.     /*
  182.      * Erase the square on screen.
  183.      */
  184.     private void erase()
  185.     {
  186.         if(isVisible) {
  187.             Canvas canvas = Canvas.getCanvas();
  188.             canvas.erase(this);
  189.         }
  190.     }
  191. }
  192.